//
//  SystemNotificationController.m
//  coba-iOS
//
//  Created by 郭四虎 on 15/11/18.
//  Copyright © 2015年 YouKu. All rights reserved.
//

#import "SystemNotificationController.h"
#import "SystemNotiCell.h"
#import "SystemNotiModel.h"
#import "SystemSecondNotiController.h"

@interface SystemNotificationController ()<UITableViewDataSource,UITableViewDelegate>
{
    UITableView *_tabview;
    NSInteger _num;
    
    BOOL _iscleaned;
}
@end

@implementation SystemNotificationController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _num = 0;
    _iscleaned = NO;
    [self initUI];
    if (!_dataArr.count) {
        [self noMessage];
    }else{
        [_tabview reloadData];
    }
    for (SystemNotiModel *model in _dataArr) {
        if (!model.isViewed) {
            _num++;
        }
    }
}

- (void)initUI
{
    _tabview = [[UITableView alloc]initWithFrame:CGRectMake(0,64, WIDTH, HEIGHT- 64)];
    
    _tabview.delegate = self;
    _tabview.dataSource = self;
    [self.view addSubview:_tabview];
    [_tabview registerNib:[UINib nibWithNibName:@"SystemNotiCell" bundle:nil] forCellReuseIdentifier:@"SystemNotiCell"];
    [_tabview delsetExtraline];
    
   
}

- (void)clearNoti
{
//    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"清空列表" message:@"确认操作" preferredStyle:UIAlertControllerStyleAlert];
//    [alert addAction:[UIAlertAction actionWithTitle:@"是" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//        
//            [[EGOCache globalCache]removeCacheForKey:[Myuser sharedUser].login_id];
//            _iscleaned = YES;
//            [_dataArr removeAllObjects];
//            [_tabview reloadData];
//            [self noMessage];
//        
//    }]];
//    [alert addAction:[UIAlertAction actionWithTitle:@"否" style:UIAlertActionStyleDefault handler:nil]];
//    [self presentViewController:alert animated:YES completion:nil];
    
    [Myuser alertWithTitle:@"清空列表" Msg:@"确认操作" target:self action:^{
        [[EGOCache globalCache]removeCacheForKey:[Myuser sharedUser].login_id];
            _iscleaned = YES;
            [_dataArr removeAllObjects];
            [_tabview reloadData];
            [self noMessage];
    }];
}

- (void)noMessage
{
    UILabel* _msgLb = [[UILabel alloc]initWithFrame:CGRectMake(20, 20, WIDTH - 40, 50)];
    _msgLb.text = @"暂时没有消息";
    _msgLb.textAlignment = NSTextAlignmentCenter;
    _msgLb.textColor = [UIColor lightGrayColor];
    _msgLb.font = [UIFont systemFontOfSize:18];
    [_tabview addSubview:_msgLb];
}

//- (void)createData
//{
//    NSLog(@"%@",NSHomeDirectory());
//    
//    NSDictionary *parameter;
//    if ([[EGOCache globalCache]objectForKey:[Myuser sharedUser].login_id])
//    {
//        parameter = @{@"user_id":[Myuser sharedUser].login_id,@"flag":@(1)};
////        NSDictionary *dic = (NSDictionary *)[[EGOCache globalCache]objectForKey:[Myuser sharedUser].login_id];
//        
////        if ([dic objectForKey:@"SystemNotification"])
////        {
////            NSArray *arr = [dic objectForKey:@"SystemNotification"];
////            if (arr.count) {
////                parameter = @{@"user_id":[Myuser sharedUser].login_id,@"flag":@(1)};
////            }else{
////                parameter = @{@"user_id":[Myuser sharedUser].login_id,@"flag":@(0)};
////            }
////            
////        }else{
////            parameter = @{@"user_id":[Myuser sharedUser].login_id,@"flag":@(0)};
////        }
//    }else{
//        parameter = @{@"user_id":[Myuser sharedUser].login_id,@"flag":@(0)};
//    }
//    
//    
//    
//    [MyAFN POST:SYSTEMNOTILIST parameters:parameter block:^(id responseObject) {
//
//        NSDictionary *response = responseObject;
//        
//        if ([[response objectForKey:@"status"]integerValue]) {
//            
//            NSMutableArray *array = [NSMutableArray array];
//            if ([[EGOCache globalCache]objectForKey:[Myuser sharedUser].login_id])
//            {
//                NSDictionary *dic = (NSDictionary *)[[EGOCache globalCache]objectForKey:[Myuser sharedUser].login_id];
//                
//                _dataArr = [dic objectForKey:@"SystemNotification"];
//                [array addObjectsFromArray:[response objectForKey:@"notViewedReplyList"]];
////                if ([dic objectForKey:@"SystemNotification"])
////                {
////                    
////                    _dataArr = [dic objectForKey:@"SystemNotification"];
////                    
////                    [array addObjectsFromArray:[response objectForKey:@"notViewedReplyList"]];
////                }else{
////                    
////                    [array addObjectsFromArray:[response objectForKey:@"notViewedReplyList"]];
////                }
//            }else{
//                [array addObjectsFromArray:[response objectForKey:@"notViewedReplyList"]];
//            }
//            
//            
//            for (NSDictionary *dic in array) {
//                
//                SystemNotiModel *model = [[SystemNotiModel alloc]init];
//                [model setValuesForKeysWithDictionary:dic];
//                
//                model.isnewnoti = YES;
//                [_dataArr addObject: model];
//            }
//            
//            [_tabview reloadData];
//        }
//        
//    }error:^(NSError *error) {
//        
//    }fromController:self];
//}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SystemNotiCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SystemNotiCell" forIndexPath:indexPath];
    SystemNotiModel *model = [_dataArr objectAtIndex:indexPath.row];
    
    [cell showModel:model];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 85;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SystemNotiModel *model = [_dataArr objectAtIndex:indexPath.row];
    
    if (!model.isViewed) {
        model.isViewed = YES;
        [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
        _num--;
    }
    
    SystemSecondNotiController *second = [[SystemSecondNotiController alloc]init];
    second.model = model;
    [self.navigationController pushViewController:second animated:YES];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    //缓存数据
    if (!_iscleaned) {
        NSDictionary *dic = @{@"SystemNotification":_dataArr};
        [[EGOCache globalCache]setObject:dic forKey:[Myuser sharedUser].login_id];
    }
    self.showM(_num);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
